package uwosh.cs342.project1;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.AdapterView;

public class AddContactActivity extends Activity implements AdapterView.OnItemSelectedListener {
	ArrayAdapter<String> spinnerAdapter;
	Spinner spinner;
	Intent receivedData, returnData;
	int requestCode;

	@Override
	public void onCreate(Bundle savedInstanceState){
		super.onCreate(savedInstanceState);
		setContentView(R.layout.addcontact_layout);
		receivedData = getIntent();
		Bundle extras = receivedData.getExtras();
		ArrayList<String> groupList = extras.getStringArrayList("uwosh.cs342.project1.groups");
		
		spinnerAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item);
		spinnerAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		
		//Populate the spinner with the passed in groups
		for (int index = 0; index < groupList.size(); index++){
			spinnerAdapter.add(groupList.get(index));
		}
		
		spinner = (Spinner) findViewById(R.id.spinnerAvailableGroups);
		spinner.setOnItemSelectedListener(this);
		spinner.setAdapter(spinnerAdapter);
		
		requestCode = extras.getInt("uwosh.cs342.project1.requestCode");
		if (requestCode == 2000){
			Button editButton = (Button)findViewById(R.id.buttonConfirmAddContact);
			editButton.setText("Edit Contact");
			spinner.setClickable(false);
			spinner.setEnabled(false);
			
			String[] contactInfo = extras.getStringArray("uwosh.cs342.project1.contactInfo");
			EditText firstNameText = (EditText)findViewById(R.id.textFirstName);
			EditText lastNameText = (EditText)findViewById(R.id.textLastName);
			EditText emailText = (EditText)findViewById(R.id.textEmail);
			EditText cellPhoneText = (EditText)findViewById(R.id.textCellPhone);
			EditText homePhoneText = (EditText)findViewById(R.id.textHomePhone);
			EditText altPhoneText = (EditText)findViewById(R.id.textAlternatePhone);
			EditText commentsText = (EditText)findViewById(R.id.textComments);
			EditText addressText = (EditText)findViewById(R.id.textAddress);
			
			firstNameText.setText(contactInfo[0]);
			lastNameText.setText(contactInfo[1]);
			emailText.setText(contactInfo[6]);
			cellPhoneText.setText(contactInfo[3]);
			homePhoneText.setText(contactInfo[4]);
			altPhoneText.setText(contactInfo[5]);
			commentsText.setText(contactInfo[7]);
			addressText.setText(contactInfo[2]);
		}
	}//end onCreate
	
	/*	The confirmAddContact method is called when the Add Contact button is
	 * 	pressed. It retrieves the information entered in the given text fields
	 * 	and stores it in an Intent object. A result code is set and then the
	 * 	current activity is finished and the results sent back to the parent.
	 * 
	 * 	I used a 4 digit ID for the result code to prevent confusion with existing
	 * 	result code constants.	*/
	public void confirmAddContact(View view){
		Intent results = new Intent();
		
		//Get views for each of the text fields
		EditText firstNameText = (EditText)findViewById(R.id.textFirstName);
		EditText lastNameText = (EditText)findViewById(R.id.textLastName);
		EditText emailText = (EditText)findViewById(R.id.textEmail);
		EditText cellPhoneText = (EditText)findViewById(R.id.textCellPhone);
		EditText homePhoneText = (EditText)findViewById(R.id.textHomePhone);
		EditText altPhoneText = (EditText)findViewById(R.id.textAlternatePhone);
		EditText commentsText = (EditText)findViewById(R.id.textComments);
		EditText addressText = (EditText)findViewById(R.id.textAddress);
		
		//Send contents of text fields back to parent activity
		results.putExtra("uwosh.cs342.project1.firstName", firstNameText.getText().toString());
		results.putExtra("uwosh.cs342.project1.lastName", lastNameText.getText().toString());
		results.putExtra("uwosh.cs342.project1.email", emailText.getText().toString());
		results.putExtra("uwosh.cs342.project1.cellPhone", cellPhoneText.getText().toString());
		results.putExtra("uwosh.cs342.project1.homePhone", homePhoneText.getText().toString());
		results.putExtra("uwosh.cs342.project1.altPhone", altPhoneText.getText().toString());
		results.putExtra("uwosh.cs342.project1.comments", commentsText.getText().toString());
		results.putExtra("uwosh.cs342.project1.groupId", spinner.getSelectedItemPosition());
		results.putExtra("uwosh.cs342.project1.address", addressText.getText().toString());
		
		setResult(6001, results);
		finish();
	}//end confirmAddContact
	
	/*	This method is called when the Cancel button is pressed. It finishes the
	 * 	current activity without passing back any information.	*/
	public void cancelAddContact(View view){
		finish();
	}//end cancelAddContact

	public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
			long arg3) {
		// TODO Auto-generated method stub
		
	}

	public void onNothingSelected(AdapterView<?> arg0) {
		// TODO Auto-generated method stub
		
	}
}//end class
